package com.vvitt.kotlinhiltcarhome.di

import android.util.Log
import com.vvitt.flowpractice.common.BASE_URL1
import com.vvitt.kotlinhiltcarhome.net.CarBrandApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

/**
 * @author please call me police uncel
 * @since 2023/5/26
 * @email 110
 * @desciption 网络module 自动注入
 **/
@InstallIn(ApplicationComponent::class)
@Module
object NetWorkModule {

    @Singleton
    @Provides
    fun provideOkHttpClient(): OkHttpClient {
//        return OkHttpClient.Builder().build()
        return OkHttpClient.Builder().addInterceptor {
            it.proceed(it.request()).apply {
                Log.e("vvitt", "request:${code()}")
                Log.e("vvitt", "requesturl:${it.request().url()}")
            }
        }.build()
    }

    @Singleton
    @Provides
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        val interceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger {
            Log.e("vvitt", it)
        })
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        return Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(BASE_URL1)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Singleton
    @Provides
    fun provideCarBrandService(retrofit: Retrofit): CarBrandApi {
        return retrofit.create(CarBrandApi::class.java)
    }

}